use std::{collections::BTreeMap, fmt::Display};

use rand::Rng;

/// 生成随机的 none_str
pub fn generate_none_str(n: usize) -> String {
    // 去掉了符号及容易混淆的字符等，比如 0, o, O, 1, l, i, I。
    const ALPHABET: &[u8] = b"abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTRVWXYZ23456789";
    let mut rng = rand::thread_rng();
    (0..n)
        .map(|_| {
            let idx = rng.gen_range(0..ALPHABET.len());
            ALPHABET[idx] as char
        })
        .collect::<String>()
}

/// 将 BTreeMap 转换为 query string
pub fn btree_map_to_query_string<K, V>(map: &BTreeMap<K, V>) -> String
where
    K: Display,
    V: Display + AsRef<str>,
{
    map.iter()
        .filter(|(_, v)| !v.as_ref().is_empty())
        .map(|(k, v)| format!("{}={}", k, v))
        .collect::<Vec<_>>()
        .join("&")
}
